package com.yswl.pri.home.av;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;

import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.yswl.pri.R;

import java.io.IOException;

import yswl.com.klibrary.base.MFragment;
import yswl.com.klibrary.browser.WebViewExtra;

public class AvPlayerFragment extends MFragment {
    private static final String TAG = AvPlayerFragment.class.getSimpleName();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_palyer, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        final ProgressBar progressBar = view.findViewById(R.id.progress);
        progressBar.setVisibility(View.VISIBLE);
        final SurfaceView surfaceView = view.findViewById(R.id.surface);
        final MediaPlayer player = new MediaPlayer();
        String url = "https://xunlei2.34885.xyz/zhubo/202107/mp4/avid60e49ac2503cc.mp4";
        SurfaceHolder.Callback callback = new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                player.setDisplay(holder);
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {

            }
        };
        try {
            surfaceView.getHolder().addCallback(callback);
            player.setDataSource(getContext(), Uri.parse(url));
            player.prepare();
            player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    progressBar.setVisibility(View.GONE);
                    player.start();
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
        loadData();
    }

    private void loadData() {

    }


}
